﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SimpleServiceBus.Bus.MessageManagement.Serialization;
using SimpleServiceBus.Endpoint.Metadata;

namespace SimpleServiceBus.Bus.Pipeline.PipelineComponents.MessageLogging
{
    public class MessageLoggingComponent : PipelineComponentBase
    {
        public MessageLoggingComponent():base("Logger")
        {
        }

        protected override void ProcessMessage(MessageEnvelope messageEnvelope, IMessageContext context)
        {
            MessageLoggingLevel logLevel = context.MessagePipeline.PipelineDirection == PipelineDirection.Receive
                                               ? context.EndpointMetadata.IncomingMessageLogLevel
                                               : context.EndpointMetadata.OutgoingMessageLogLevel;
            
            var logger = context.GetLocalService<IMessageLogger>();
            if (logger != null)
                logger.LogMessage(context, logLevel);
        }
    }
}